package com.volcreate.inn.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.volcreate.inn.mapper.TagMapper;
import com.volcreate.inn.po.Tag;
import com.volcreate.inn.service.ITagService;
import com.volcreate.inn.utils.MyListAndStringUtils;
import com.volcreate.inn.vo.TagVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zxc
 * @since 2020-08-23
 */
@Service
public class TagService extends ServiceImpl<TagMapper, Tag> implements ITagService {

    @Autowired
    private TagMapper tagMapper;

    @Override
    public Tag getByName(String name) {
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper
                .eq("name", name);
        return tagMapper.selectOne(wrapper);
    }

    @Override
    public PageInfo<Tag> list(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(tagMapper.selectList(null));
    }

    @Override
    public List<Tag> list(String tagIds) {
        List<Tag> list = new ArrayList<>();
        List<Long> longs = MyListAndStringUtils.stringToList(tagIds);
        for (Long aLong : longs) {
            list.add(tagMapper.selectById(aLong));
        }
        return list;
    }

    @Override
    public List<TagVO> listByBlogSizeDesc() {
        return tagMapper.selectByBlogSizeDesc();
    }

    @Override
    public PageInfo<TagVO> listByBlogSizeDesc(int page, int size) {
        PageHelper.startPage(page, size);
        return new PageInfo<>(tagMapper.selectByBlogSizeDesc());
    }

    @Override
    public PageInfo<Tag> listByGmtModifiedDesc(int page, int size) {
        PageHelper.startPage(page, size);
        QueryWrapper<Tag> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("gmt_modified");
        return new PageInfo<>(tagMapper.selectList(wrapper));
    }
}
